/**
 * 2010-5-13
 * InnovationServer
 * @author lyh
 */
package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.instancemanager.QuestManager;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.model.quest.dataset.QuestDataSet;
import labox.innovation.gameserver.network.serverpackets.NpcHtmlMessage;
import labox.innovation.gameserver.templates.item.L2Item;

public class RequestQuestSelect extends L2GameClientPacket {
	private static final String _C__52_REQUESTQUESTSELECT = "[C] 52 RequestQuestSelect";

	private static final Logger _log = LoggerFactory.getLogger(RequestQuestSelect.class.getName());

	private String _command;
	
	private static String[] silverArray = {"银文","银票","元宝","积分"};

	@Override
	public String getType() {
		return _C__52_REQUESTQUESTSELECT;
	}

	@Override
	protected void readImpl() {
		String str = readString();
		_command = str.replaceAll("\"", "");
	}

	@Override
	protected void runImpl() {
		StringBuilder sb = new StringBuilder();
		int rewardsExp;  //-- 奖励经验
		int rewardsCurrencyType;  //-- 奖励货币类型
		int rewardsCurrencyAmount;  //-- 奖励货币数量
		int rewardssw;  //-- 奖励声望
		String rewardsTitle;  //-- 奖励称号
		String[] rewardsItems;  //-- 奖励物品及数量
		String[] rewardsItemArray;
		String[] rewardsOptionalItems;  //-- 可选道具及数量
		String[] rewardsOptionalItemArray;
//		String rewardsSkillId;  //-- 奖励技能
		FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;
		try {
			if (_command.length() > 0) {
				// 查看任务
				String qn = _command.trim();
				Quest qe = QuestManager.getInstance().getQuest(qn);
				if (qe != null) {
					String diff = QuestDataSet.QuestDifficulty.getQuestDifficulty(qe.getQuestDataSet().getQuestDifficulty()).getName();
					if ("困难".equals(diff)) {
						sb.append("<form w='80%' h='80%' ol='back()'>").append(qe.getDescr()).append("[").append(diff).append("]<br/><br/>");
					} else {
						sb.append("<form w='80%' h='80%' ol='back()'>").append(qe.getDescr()).append("<br/><br/>");
					}
					
					String str = "";
					if (activeChar.getQuestState(qn) != null) { // 判断任务是否已接
						sb.append(qe.getQuestDataSet().getQuestInfo()).append("<br/>");
						rewardsExp = qe.getQuestDataSet().getRewardsExp();  //-- 获取经验值
						if (rewardsExp > 0)
							sb.append("<a c='00FFFF00'>奖励经验：</a>").append(rewardsExp);
						rewardsCurrencyAmount = qe.getQuestDataSet().getRewardsCurrencyAmount();  //-- 获取奖励货币数量
						
						if (rewardsCurrencyAmount > 0) {
							sb.append("<br/><a c='00FFFF00'>奖励");
							rewardsCurrencyType = qe.getQuestDataSet().getRewardsCurrencyType();
							if (rewardsCurrencyType >=0 && rewardsCurrencyType <=3)
							    sb.append(silverArray[rewardsCurrencyType]).append("：");  //-- 获取奖励货币类型
							else sb.append(silverArray[0]).append("：");
							
							sb.append("</a>").append(rewardsCurrencyAmount);
						}
						
						rewardssw = qe.getQuestDataSet().getRewardssw();  //-- 奖励声望
						
						if (rewardssw > 0)
							sb.append("<br/><a c='00FFFF00'>奖励声望：</a>").append(rewardssw);
						
						rewardsTitle = qe.getQuestDataSet().getRewardsTitle();  //-- 获取奖励称号
						
						if (rewardsTitle != null && !"".equals(rewardsTitle))
							sb.append("<br/><a c='00FFFF00'>奖励称号：</a>").append(rewardsTitle).append("<br/>");
						
						if (qe.getQuestDataSet().getRewardsItem() != null && !"".equals(qe.getQuestDataSet().getRewardsItem()) && !"-1".equals(qe.getQuestDataSet().getRewardsItem())) {
							sb.append("<tb ln='n'> <tr>");
							sb.append("<td w='70' ><a c='00FFFF00'>物品奖励：</a></td> <td>");
							rewardsItems = qe.getQuestDataSet().getRewardsItem().split("~");
							
							for (int i = 0; i < rewardsItems.length; i++) {
								rewardsItemArray = rewardsItems[i].split(",");
							    L2Item item =	ItemTable.getInstance().getTemplate(Integer.valueOf(rewardsItemArray[0]));
							    
							    if (item != null && item.getName() != null && !"".equalsIgnoreCase(item.getName())) {
								    sb.append("<a cmd='send:113:b1:i").append(rewardsItemArray[0]).append("'>").append(item.getName()).append("X").append(rewardsItemArray[1]).append("</a><br/>");
							    }
							}
							
							sb.append("</td></tr></tb>");
						}
						
						//-- 选择奖励
						if (qe.getQuestDataSet().getRewardsOptionalItem() != null && !"".equals(qe.getQuestDataSet().getRewardsOptionalItem()) && !"-1".equals(qe.getQuestDataSet().getRewardsOptionalItem())) {
							sb.append("<tb ln='n'> <tr>");
							sb.append("<td w='70' ><a c='00FFFF00'>选择奖励：</a></td> <td>");
							rewardsOptionalItems = qe.getQuestDataSet().getRewardsOptionalItem().split("~");
							
							for (int i = 0; i < rewardsOptionalItems.length; i++) {
								rewardsOptionalItemArray = rewardsOptionalItems[i].split(",");
								sb.append("<a cmd='send:113:b1:i").append(rewardsOptionalItemArray[0]).append("'>").append(ItemTable.getInstance().getTemplate(Integer.valueOf(rewardsOptionalItemArray[0])).getName()).append("X").append(rewardsOptionalItemArray[1]).append("</a><br/>");
							}
							
							sb.append("</td></tr></tb>");
						}		
					} else {
						str = qe.getQuestDataSet().getTaskBriefing();// 任务提示
						
						if (str == null || str.equals(""))
							sb.append("无可奉告!");
						else
							sb.append(str);
					}
						
					sb.append("<br/></form>");
					sendPacket(new NpcHtmlMessage(5, sb.toString()));
				} else {
					_log.warn("Quest (name='{" + qn + "}') not found.", Config.SERVER_ID, "选择任务");
				}
			}
		} catch (Exception e) {
			_log.error("{" + getClient() + "} sent bad RequestQuestSelect: ", Config.SERVER_ID, e);
		}
	}
}
